package io.e4x.example.jnidemo.service;

import com.sun.jna.ptr.ByteByReference;
import io.e4x.example.jnidemo.FudxCall;
import io.e4x.example.jnidemo.config.JNAConfig;
import org.springframework.stereotype.Component;

@Component
public class StreamConnectorImpl implements StreamConnector {
    private FudxCall fudx;

    StreamConnectorImpl(JNAConfig jnaConfig){
        System.load(jnaConfig.fudxPath);
    }

    @Override
    public void connect(String server, int port, String name) {
//        System.load("C:\\Users\\sukim\\Documents\\code\\jnidemo\\libs\\fudxCall.dll");
        FudxCall.instance.delegate(new FudxCall.OnConnectedListener() {
            @Override
            public void invoke(int stat) {
                connected(stat);
            }
        }, new FudxCall.OnStreamReadListener() {
            @Override
            public void invoke(ByteByReference data, int len, int type) {
                onReadData(data.getPointer().getByteArray(0, len), len, type);
            }
        });
        FudxCall.instance.registServer(server, port, name);
    }
    private void connected(int stat) {
        System.out.println("onConnected " + stat);
    }
    private void onReadData(byte[] data, int len, int type) {
        System.out.println("onReadData " + data);
    }
}
